package com.truyentranh.main;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.truyentranh.controls.AdsViewControl;
import com.truyentranh.data.AdsDataObject;
import com.truyentranh.modules.haivl.HaiVLActivity;
import com.truyentranh.network.APIRequestWorker;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.requester.AdsRequester;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.Logger;

@SuppressLint("SetJavaScriptEnabled")
public class AdsBussiness {
	private final static boolean TESTING = false;
	private static AdsBussiness instance = null;
	
	
	private static final int GOOGLE_MOB = -1;
	private static final int WEB_VIEW_ADS = -2;
	private static final int IMAGE_VIEW_ADS = -3;
	
	private static final int CURRENT_INDEX_ADS = 0;
	
	private  ArrayList<AdsDataObject> adsDataObjects = null;
			
	Activity activity;
	ViewGroup containerView;

	public static AdsBussiness getInstance() {
		if (instance == null)
			instance = new AdsBussiness();
		return instance;
	}
	
	public void startTickerAds(){
		Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. startTickerAds");
		if(adsDataObjects != null && !adsDataObjects.isEmpty()){
			Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. startTickerAds 1");
			if(adsDataObjects.size() == 1){
				Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. startTickerAds == 1");
				final AdsDataObject adsDataObject = adsDataObjects.get(0);
				Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. adsDataObject name "+adsDataObject.getName());
				if(activity != null && containerView != null){					
					if(adsDataObject.getType() == WEB_VIEW_ADS){
						activity.runOnUiThread(new Runnable() {
							
							@SuppressLint("NewApi")
							public void run() {
								Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. adsDataObject WEB_VIEW_ADS ");
								WebView webView = new WebView(activity);
								if(webView != null){
									Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. new WEB_VIEW_ADS ");
//									webView.loadUrl(adsDataObject.getContent());
//									
//									byte[] data = Base64.decode(adsDataObject.getContent(), 0);
									String data = "<a target=\"_blank\" href=\"http://b.123c.vn/clk/?params=FpBXcb5R97bRM9hWT6vDCJTUYaXJ94wB2ioZQ3F9ByiuuTDY0PTGr0c8r4aVk7VlGRwS3ZpB9RKrbR9r3kbcdY30WtOBz3l-K82CFos7VT4m_nSdNfVovWJa7dngygguHQi2PtpvlxbTk0e85joIz1cyKK5UMIRNwjvfLqqLR0\"><img src=\"http://img.click.zdn.vn/10795.jpg\" title=\"Zalo on Top News\"></a><img src=\"http://hit.123c.vn/report/zads-1x1.gif?zid=1f98ef58d31d3a43630c&ziid=be9c295e151bfc45a50a&t=1363570737534\">";
									if(data != null){
										webView.loadData(data,"text/html","");
										webView.setMinimumHeight(50);
										webView.setMinimumWidth(320);
										
										webView.setPadding(0, 0, 0, 0);
										webView.setInitialScale(100);
										webView.getSettings().setJavaScriptEnabled(true);
										Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. add WEB_VIEW_ADS ");
										containerView.addView(webView);
									}
									
								}								
							}
						});
						
					}
				}
			} else {
				//start ticker, each 1 min
			}
		}
	}

	AdView mainAd = null;
	private int countToRequestAds = 0;
	public void doGetAdsFromServer(final Activity activity, final AdsViewControl containerView) {
		if(AdsViewControl.NO_ADS) return;
		
		this.countToRequestAds++;
		this.activity = activity;
		this.containerView = containerView;
		
		if (AppSettingParams.isUseGoogleAds()) {
			if (activity != null && containerView != null) {				
				activity.runOnUiThread(new Runnable() {
					public void run() {
						boolean isAddLoadRequest = false;
						if(mainAd == null){
							isAddLoadRequest = true;
							mainAd = new AdView(activity, AdSize.BANNER, AppSettingParams.pr_publish_id);
						} else {
							((AdsViewControl)mainAd.getParent()).removeView(mainAd);
						}
						containerView.removeAllViews();
						containerView.addView(mainAd);
						
						//check 1 dk huu han nao do
						Logger.logMe("countToRequestAds = "+countToRequestAds);
						if(countToRequestAds >= 5){
							countToRequestAds = 0;
							isAddLoadRequest = true;
						}
						
//						if(isAddLoadRequest){
							AdRequest adRequest = new AdRequest();
							adRequest.setTesting(TESTING);
							mainAd.loadAd(adRequest);
							containerView.setVisibility(View.VISIBLE);
//						}
						
//						AdView ad = new AdView(activity, AdSize.BANNER, AppSettingParams.pr_publish_id);
//						
//						containerView.removeAllViews();
//						containerView.addView(ad);
//						
//						AdRequest adRequest = new AdRequest();
//						adRequest.setTesting(TESTING);
//						ad.loadAd(adRequest);

						if (containerView.getVisibility() != View.VISIBLE)
							containerView.setVisibility(View.VISIBLE);
					}
				});
			}

		} else {
			if(adsDataObjects == null){
				AdsRequester requester = new AdsRequester(new RequestListener() {

					public void onRequestFailed(String errorMsg, Request request) {

					}

					public void onRequestComplete(Request request) {
						Logger.logMe("request");
						if (request instanceof AdsRequester) {
							AdsRequester r = (AdsRequester) request;
							Logger.logMe("r.useGoogle() = "+r.useGoogle());
							if (r.useGoogle()) {
								AppSettingParams.isInitAds = true;
								AppSettingParams.useGoogleAds = r.useGoogle();
								if (activity != null && containerView != null) {
									activity.runOnUiThread(new Runnable() {
										public void run() {
											boolean isAddLoadRequest = false;
											if(mainAd == null){
												isAddLoadRequest = true;
												mainAd = new AdView(activity, AdSize.BANNER, AppSettingParams.pr_publish_id);
											} else {
												((AdsViewControl)mainAd.getParent()).removeView(mainAd);
											}
											containerView.removeAllViews();
											containerView.addView(mainAd);
											
											//check 1 dk huu han nao do
											
											if(isAddLoadRequest){
												AdRequest adRequest = new AdRequest();
												adRequest.setTesting(TESTING);
												mainAd.loadAd(adRequest);
												containerView.setVisibility(View.VISIBLE);
											}											
										}
									});
								}
							} else {
								Logger.logMe(">>>>>>>>>>>>>>>>>>>>>>. show a web");
								adsDataObjects = new ArrayList<AdsDataObject>();
								adsDataObjects = r.getArrayList();		
								startTickerAds();
							}
						}

					}
				});
				APIRequestWorker.getInstance().addAPIRequest(requester);
			} else {
				startTickerAds();
			}
		}
	}
}
